package com.lhy.noed;

import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * @description: 拼接sql
 * @author: 李汇远
 */
public class SqlBuilder {

    private DbInfo dbInfo;

    public SqlBuilder(DbInfo dbInfo) {
        this.dbInfo = dbInfo;
    }

    /**
     * 获取插入新记录的sql语句
     *
     * @param tableName 表格名称
     * @param data      对应的数据
     * @return
     */
    public String getInsertSql(String tableName, JSONObject data) {
        String template = "insert into %s(%s) values(%s)";
        Map<String, List<String>> colNames = dbInfo.getColNames();
        StringBuilder colSql = new StringBuilder();
        StringBuilder colDataSql = new StringBuilder();
        // 遍历字段列表
        for (String colName : colNames.get(tableName)) {
            String tmpData = data.getString(colName);
            if (tmpData == null)
                continue;
            colSql.append('`');
            colSql.append(colName);
            colSql.append("`,");
            colDataSql.append(String.format("'%s',", tmpData));
        }
        if (colSql.length() == 0 || colDataSql.length() == 0)
            throw new NullPointerException("不能插入空数据, 数据库表:" + tableName);
        colSql.deleteCharAt(colSql.length() - 1);
        colDataSql.deleteCharAt(colDataSql.length() - 1);
        return String.format(template, tableName, colSql, colDataSql);
    }

    /**
     * 根据指定字段名的sql
     *
     * @param tableName 表名
     * @param key       字段名
     * @param value     字段的值
     * @return
     */
    public String getDeleteSql(String tableName, String key, String value) {
        if (key == null || "".equals(key))
            throw new NullPointerException("删除操作的字段名不能为空,数据库表:" + tableName);
        String template = "delete from `%s` where `%s`='%s'";
        return String.format(template, tableName, key, value);
    }

    /**
     * 删除字段为空值的
     *
     * @param tableName
     * @param key
     * @return
     */
    public String getDeleteSql(String tableName, String key) {
        if (key == null || "".equals(key))
            throw new NullPointerException("删除操作的字段名不能为空,数据库表:" + tableName);
        String template = "delete from `%s` where `%s` is null";
        return String.format(template, tableName, key);
    }

    /**
     * 查询所有记录
     *
     * @param tableName
     * @return
     */
    public String getSelectSql(String tableName) {
        return "select * from " + tableName;
    }

    /**
     * 根据指定where条件查询
     *
     * @param tableName
     * @param where
     * @return
     */
    public String getSelectSql(String tableName, String where) {
        if (where == null || "".equals(where)) {
            throw new NullPointerException("查询操作的where条件不能为空,数据库表:" + tableName);
        }
        return String.format("select * from `%s` where %s", tableName, where);
    }

    /**
     * 查询指定语句
     *
     * @param tableName
     * @param key
     * @param val
     * @return
     */
    public String getSelectSql(String tableName, String key, String val) {
        return getSelectSql(tableName, String.format("`%s`='%s'", key, val));
    }

    /**
     * 根据参数查询
     *
     * @param tableName
     * @param params
     * @return
     */
    public String getSelectSql(String tableName, JSONObject params) {
        StringBuilder col = new StringBuilder();
        for (Map.Entry entry : params.entrySet()) {
            col.append(String.format("and `%s`='%s' ", entry.getKey(), entry.getValue()));
        }
        return getSelectSql(tableName, col.substring(3));
    }

    public String getUpdateSql(String tableName, String content, String where) {
        if (content == null || "".equals(content) || where == null || "".equals(where)) {
            throw new NullPointerException("更新操作的内容和条件不能空, 数据库表:" + tableName);
        }
        String template = "update `%s` set %s where %s";
        return String.format(template, tableName, content, where);
    }

    /**
     * 获取更新的sql
     *
     * @param tableName
     * @param params
     * @return
     */
    public String getUpdateSql(String tableName, JSONObject params, String where) {
        Map<String, List<String>> colNames = dbInfo.getColNames();
        StringBuilder colSql = new StringBuilder();
        // 遍历字段列表
        for (String colName : colNames.get(tableName)) {
            String tmpData = params.getString(colName);
            if (tmpData == null)
                continue;
            colSql.append(",`%s`='%s'");
        }
        return getUpdateSql(tableName, colSql.substring(1), where);
    }

    public String getUpdateSql(String tableName, String colName, String colData, String where) {
        return getUpdateSql(tableName, String.format("`%s`='%s'", colName, colData), where);
    }


}
